package com.huihui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.huihui.R;
import com.huihui.bean.MinePingLun;
import com.huihui.datamanager.AppManager;
import com.huihui.utils.LoadImageUtil;

import java.util.List;


/**
 * Created by Administrator on 2018/8/18.
 */

public class MineMsgSayAdapter extends RecyclerView.Adapter<ItemViewHolder1>{


    private Context context;
    private List<MinePingLun> list;

    public MineMsgSayAdapter(Context context, List<MinePingLun> list){
        this.context = context;
        this.list = list;
    }


    @Override
    public com.huihui.adapter.ItemViewHolder1 onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mine_msg_say, parent, false);
        return new com.huihui.adapter.ItemViewHolder1(view);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder1 holder, int position) {
        MinePingLun pingLun = list.get(position);
        if(pingLun != null){
            //设置数据
            //评论人
            String send_user_nicknameStr = pingLun.getSend_user_nickname();
            String reply_commentStr = pingLun.getReply_comment();
            LoadImageUtil.displayImage(context,pingLun.getSend_user_headimg(),holder.send_user_headimg);
            holder.send_user_nickname.setText(pingLun.getSend_user_nickname()+pingLun.getAction_name());
            holder.msg_time.setText(pingLun.getMsg_time());

            //作者
            holder.author_conent.setText(pingLun.getAuthor_conent());
            holder.author_nickname.setText(pingLun.getAuthor_nickname());
            String currentComment = pingLun.getCurrent_comment();
            if(TextUtils.isEmpty(reply_commentStr)){
                holder.reply_comment_layout.setVisibility(View.GONE);
            }else {
                holder.reply_comment_layout.setVisibility(View.VISIBLE);
                holder.reply_comment.setText(AppManager.getUserInfo().getNick_name()+":"+reply_commentStr);
            }
            int class_id = pingLun.getClass_id();
            if (class_id == 1) {
                holder.zan_playIcon.setVisibility(View.VISIBLE);
            } else {
                holder.zan_playIcon.setVisibility(View.GONE);
            }
            holder.current_content.setText(currentComment);
            LoadImageUtil.displayImage(context,pingLun.getAuthor_headimg(),holder.author_headimg);
            if(replyListener != null){
                holder.huifu.setTag(position);
                holder.huifu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int p = (int) v.getTag();
                        replyListener.onClick(p);
                    }
                });
            }
        }
    }


    private OnItemClickListener onItemClickListener;
    private OnReplyListener replyListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public interface OnReplyListener{
        void onClick(int position);
    }

    public void setOnReplyClickListener (OnReplyListener listener) {
        this.replyListener = listener;
    }
}
